import { BodyParam, Ctx, Delete, Get, JsonController, Param, Post, Put, QueryParam } from "routing-controllers"
import UserService from "../services/UserService"
import Response from '../common/response';
import { Inject } from "typedi"
import { Context } from 'koa';
import CoutomError from '../common/CoutomError';

@JsonController("/user")
export default class UserController {
    @Inject()
    userService: UserService
    @Post()
    async add(
        @BodyParam("username", { required: true }) username: string,
        @BodyParam("password", { required: true }) password: string,
        @BodyParam("phone", { required: true }) phone: string,
        @BodyParam("avatar", { required: true }) avatar: string,
        @BodyParam("nickname", { required: true }) nickname: string,
        @BodyParam("roleIds") roleIds: number[]) {
        let user = await this.userService.selectUserByUsername(username)
        if (user) {
            return Response.BAD_REQUEST("用户名已存在")
        }
        await this.userService.addUser(username, password, nickname, phone, avatar, roleIds)
        return Response.SUCCESS()
    }

    @Get("")
    async all(@QueryParam("username") username: string,
        @QueryParam("phone") phone: string,
        @QueryParam("updateTimeRange") updateTimeRange: string[],
        @QueryParam("createTimeRange") createTimeRange: string[],
        @QueryParam("deptId") deptId: number,
        @QueryParam("roleCodes", { parse: true }) roleCodes: string[],
        @QueryParam("page") page: number,
        @QueryParam("pageSize") pageSize: number) {
        let result = await this.userService.getUserByConds({
            username,
            phone,
            createTimeRange,
            updateTimeRange,
            deptId,
            roleCodes,
            page,
            pageSize
        })
        return Response.SUCCESS(result)
    }
    @Put()
    async update(
        @BodyParam("id", { required: true }) id: number,
        @BodyParam("username") username: string,
        @BodyParam("password") password: string,
        @BodyParam("nickname") nickname: string,
        @BodyParam("phone") phone: string,
        @BodyParam("deptId") deptId: number,
        @BodyParam("avatar") avatar: string,
        @BodyParam("dataStatus") dataStatus: number,
        @BodyParam("roleIds") roleIds: number[]) {
        let user = await this.userService.selectUserByUsername(username)
        if (user && user.id != id) {
            return Response.BAD_REQUEST("用户名已存在")
        }
        let isUpdate = await this.userService.updateUser({
            id,
            username,
            dataStatus,
            deptId,
            nickname,
            password,
            phone,
            avatar,
            roleIds
        })
        if (isUpdate) {
            return Response.SUCCESS()
        }
        return Response.BAD_REQUEST("更新失败")
    }
    @Put("/changePassword")
    async changePassword(@Ctx() ctx: Context,
        @BodyParam("password", { required: true }) password: string,
        @BodyParam("newPassword", { required: true }) newPassword: string
    ) {
        try {
            await this.userService.changePassword(ctx, password, newPassword)
        } catch (error) {
            if (error instanceof CoutomError) {
                return Response.BAD_REQUEST(error.message)
            }
            throw error
        }
        return Response.SUCCESS()
    }
    @Get("/getUserById/:id")
    async getUserById(@Param("id") id: number) {
        let user = await this.userService.getUserById(id)
        return Response.SUCCESS(user)
    }
    @Get("/userInfo")
    async getAll(@Ctx() ctx: Context) {
        return Response.SUCCESS(ctx.userInfo)
    }
    @Delete("/:id")
    async delUser(@Param("id") id: number) {
        try {
            await this.userService.delUser(id)
        } catch (err) {
            return Response.BAD_REQUEST(err.message)
        }
        return Response.SUCCESS()
    }
}
